<?php
/**
 * 客户模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;
use think\Db;

class CompanyUser extends BaseModel {

    public static $status = [
        0 => '解绑客户',
        1 => '潜在客户',
        2 => '专属客户',
    ];

    /**
     * 客户列表
     * @param $params
     * @return mixed
     */
    public static function getList($params) {
        empty($params['page']) ? $page = 1 : $page = $params['page'];
        empty($params['long']) ? $long = 10 : $long = $params['long'];
        $column = ['cau.id', 'cu.cover', 'cau.status', 'cau.remark', 'cau.phone', 'cau.remark_phone', 'ca.name as ca_name', 'ch.name as ch_name', 'if(cau.status=2,cau.itime_exclusive,cau.itime_potential) as itime', 'cu.nickname', 'cu.openid', 'clc.id as hasConsult'];
        $order = ['itime' => 'desc'];
        if ($params['account_type'] == 0) {//公司账号
            //选择了门店
            if (isset($params['shop_id']) && $params['shop_id'] != '') {
                $map[] = ['ca.company_shop_id', "=", $params['shop_id']];
                $map[] = ['ca.company_id', "=", $params['company_id']];
            } else {
                $map[] = ['ca.company_id', "=", $params['company_id']];
            }
        } else {
            $map[] = ['ca.company_shop_id', "=", $params['company_shop_id']];
        }
        //客户名称
        if (!empty($params['cu_name'])) {
            $map[] = ['cau.remark', "like", "%" . trim($params['cu_name']) . "%"];
        }
        //经济人名称
        if (!empty($params['ca_name'])) {
            $map[] = ['ca.name', "like", "%" . trim($params['ca_name']) . "%"];
        }
        //经纪人id
        if (!empty($params['ca_id'])) {
            $map[] = ['cau.agent_id', "=", $params['ca_id']];
        }
        //客户状态
        if (!empty($params['status'])) {
            $map[] = ['cau.status', "=", (int)$params['status']];
        }
        $map[] = ['ca.deleted', '=', 0];
        $data = self::list_query($map, $order, $page, $long, $column);
        $data['data'] = self::list_style($data['data']);
        return $data;
    }

    /**
     * 列表sql
     * @param $map
     * @param $order
     * @param $page
     * @param $long
     * @param $column
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function list_query($map, $order, $page, $long, $column) {
        $count = db("company_agent_user")
            ->alias('cau')
            ->leftJoin('company_user cu', 'cau.user_id=cu.id')
            ->leftJoin('company_agent ca', 'cau.agent_id = ca.id')
            ->leftJoin('company_shop ch', 'ch.id = ca.company_shop_id')
            ->leftJoin("company_loan_consult clc", 'clc.user_id = cau.user_id')
            ->fieldRaw("(" . Db::name('agent_user_demand')
                    ->alias('aud')
                    ->where('aud.user_id', 'cau.user_id')
                    ->fetchSql(true)->count() . ") as hasDemand")
            ->where($map)
            ->count();
        if ($count == 0) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $data = db("company_agent_user")
                ->alias('cau')
                ->leftJoin('company_user cu', 'cau.user_id=cu.id')
                ->leftJoin('company_agent ca', 'cau.agent_id = ca.id')
                ->leftJoin('company_shop ch', 'ch.id = ca.company_shop_id')
                ->leftJoin("company_loan_consult clc", 'clc.user_id = cau.user_id')
                ->fieldRaw("(" . Db::name('agent_user_demand')
                        ->where('user_id', 'cau.user_id')
                        ->fetchSql(true)->count() . ") as hasDemand")
                ->where($map)
                ->order($order)
                ->page($page, $long)
                ->field($column)
                ->select();
            $pageNum = ceil($count / $long);
            $result = [
                'page'    => (int)$page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 列表样式
     * @param $data
     * @return mixed
     */
    public static function list_style($data) {
        foreach ($data as $k => $v) {
            $data[$k]['itime'] = date("Y-m-d H:i:s", $data[$k]['itime']);
            $data[$k]['nickname'] = base64_decode($data[$k]['nickname']);//客户昵称
            $data[$k]['status'] = self::$status[$data[$k]['status']];//客户类型
            $data[$k]['ch_name'] = empty($data[$k]['ch_name']) ? '直属公司' : $data[$k]['ch_name'];//门店名称
            $data[$k]['hasDemand'] = empty($data[$k]['hasDemand']) ? 0 : 1;//是否有需求
            $data[$k]['hasConsult'] = empty($data[$k]['hasConsult']) ? 0 : 1;//是否有贷款
            if ($data[$k]['status'] == '潜在客户') {//潜在客户
                $data[$k]['hasDemand'] = 0;
            }
        }
        return $data;
    }

}